﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Timers;

namespace DemoBrick
{
    class Game
    {
        private const int GameWidth = 1000;
        private const int GameHeight = 700;
        private List<Brick> Bricks { get; set; }

        /// <summary>
        /// 游戏自带的Timer
        /// </summary>
        private System.Timers.Timer Timer { get; set; }
        //定义委托 
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;

        private Point[] brickPoints =
        {
            new Point (100,100),
            new Point (250,100),
             new Point (400,100),
            new Point (550,100),
             new Point (700,100),
            new Point (850,100),
             new Point (100,300),
            new Point (250,300),
             new Point (400,300),
            new Point (550,300),
             new Point (700,300),
            new Point (850,300),
        };

        private Bar _bar;
        private Ball _ball;

        public Game ()
        {
            _bar = new Bar(GameWidth);

            _ball = new Ball(new Rectangle(475,550,50,50));

            //初始化Bricks
            Bricks = new List<Brick>();
            foreach(var brickPoint in brickPoints)
            {
                Brick brick = new Brick(new Rectangle(brickPoint, new Size(100, 20)));
                Bricks.Add(brick);
            }

            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
        }

        private void Timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
        {
            //时钟处理程序
            _ball.Move (new Size (GameWidth ,GameHeight) );
            //处理碰撞
            HandleTouch();
            //通知form游戏改变了
            gameChanged();
        }


        /// <summary>
        /// 处理碰撞
        /// </summary>
        private  void HandleTouch()
        {
            if (_ball.Rectangle .IntersectsWith (_bar.Rectangle ))
            {
                //小球和bar撞上了
                _ball.ChangeVerdiction();
            }
            Brick brickNow = null;
            foreach (var brick in Bricks )
            {
                if (_ball.Rectangle.IntersectsWith(brick.Rectangle))
                {
                    //小球和brick撞上了
                    _ball.ChangeVerdiction();
                    brickNow = brick;//记下来
                    break;
                }
                
            }
            if (brickNow != null)
            {
                Bricks.Remove(brickNow);
            }
        }


        /// <summary>
        /// 绘图游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g, Size size )

        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);
            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));

            g.DrawImage(Properties.Resources.bg, rec);

            _bar.Draw(_g);
            _ball.Draw(_g);
            foreach (var brick in Bricks)
            {
                brick.Draw(_g);
               
            }

            //判断游戏胜利
            if (Bricks.Count == 0)
            {
                Timer.Stop();
                System.Windows.Forms.MessageBox.Show("游戏胜利");
            }

            //todo
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }

        internal void Draw(object graphics, Size size)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            _bar.KeyDown(key);
        } 
    }
}
